package sun.flower.leetcode;

/**
 * @Author :tuhan
 * @Date : 2021/3/2 17:12
 * 给定一个二维矩阵，计算其子矩形范围内元素的总和，该子矩阵的左上角为 (row1, col1) ，右下角为 (row2, col2) 。
 **/
public class NumMatrix {

    private int[][] matrix ;

    public NumMatrix(int[][] matrix) throws Exception {
        if(matrix.length==0){
            this.matrix=matrix;
            return;
        }
        int col = matrix[0].length;
        for(int i = 0 ;i< matrix.length;i++){
            if(col != matrix[i].length){
                throw new RuntimeException("不是矩阵");
            }

        }
        this.matrix=matrix;
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        int sum = 0;
        int firstCol1 = col1;
        if(row1<=row2 && col1<=col2){
            if(row2< matrix.length && col2< matrix[0].length){
                for( ; row1<=row2; row1++){
                    //重置列
                    col1 = firstCol1;
                    for(; col1<=col2 ; col1++){
                        sum+=matrix[row1][col1];
                    }
                }
            }
        }
        return sum;
    }

    public static void main(String[] args) throws Exception {

        int[][] matrix = {

        };
        NumMatrix numMatrix = new NumMatrix(matrix);
        int sum = numMatrix.sumRegion(0,1,2,3);
        System.out.println(sum);



    }
}






